package com.qf.electronic.mall.user.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qf.constant.CookieConstant;
import com.qf.constant.RabbitmqConstant;
import com.qf.constant.RedisConstant;
import com.qf.dto.ResultBean;
import com.qf.service.IUserService;
import com.qf.util.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 作者：张健
 * 项目名：electronic-mall
 * 时间：2020/3/10  15:45
 * 描述：
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials = "true")
@RequestMapping("user")
public class UserController {

    @Reference
    private IUserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("loginUI")
    public String loginUI() {
       return "login";
    }

    @RequestMapping("registerUI")
    public String register() {
        return "register";
    }

    @ResponseBody
    @RequestMapping("register")
    public ResultBean register(String phone, String code, String password) {
        ResultBean resultBean = userService.register(phone, code, password);
        return resultBean;
    }

    @ResponseBody
    @RequestMapping("getCode/{phone}")
    public ResultBean getcode(@PathVariable String phone) {
        rabbitTemplate.convertAndSend(RabbitmqConstant.SMS_TOPIC_EXCHANGE, "sms.send.message", phone);
        return ResultBean.success();
    }

    @ResponseBody
    @RequestMapping("email/register")
    public ResultBean mailRegister(String email,String password) {
        ResultBean resultBean = userService.mailRegister(email, password);
        return resultBean;
    }

    // http://localhost:9080/user/mail/active/account/
    @ResponseBody
    @RequestMapping("mail/active/account/{uuid}")
    public ResultBean mailActiveAccount(@PathVariable String uuid) {
        ResultBean resultBean = userService.mailActiveAccount(uuid);
        return resultBean;
    }

    //用户登录
    @RequestMapping("login")
    @ResponseBody
    public ResultBean login(String username, String password, HttpServletResponse response) {
        ResultBean resultBean = userService.login(username, password);
        if (resultBean.getErrno() == 0) {
            String uuid = UUID.randomUUID().toString();
            Cookie cookie = new Cookie(CookieConstant.USER_LOGIN, uuid);
            String redisKey = StringUtils.getRedisKey(RedisConstant.USER_LOGIN, uuid);
            redisTemplate.opsForValue().set(redisKey, resultBean.getData(), 30, TimeUnit.MINUTES);
            cookie.setMaxAge(60 * 30);
            cookie.setPath("/");
            cookie.setHttpOnly(true);
            response.addCookie(cookie);
            // "redirect:http://localhost:9081/index";

        }
        return resultBean;
    }

    @RequestMapping("checkIsLogin")
    @ResponseBody
    public ResultBean checkIsLogin(@CookieValue(name = CookieConstant.USER_LOGIN, required = true) String uuid) {
        if (uuid==null||"".equals(uuid)){
            return ResultBean.error("用户未登录");
        }
        System.out.println("uuid = [" + uuid + "]");
        ResultBean resultBean = userService.checkIsLogin(uuid);
        System.out.println("resultBean = " + resultBean);
        return resultBean;
    }
    @RequestMapping("logout")
    public String logout(@CookieValue(name = CookieConstant.USER_LOGIN, required = true) String uuid,HttpServletResponse response) {
        if (uuid!=null||!"".equals(uuid)){
            String redisKey = StringUtils.getRedisKey(RedisConstant.USER_LOGIN, uuid);
            redisTemplate.delete(redisKey);
        }
        Cookie cookie = new Cookie(CookieConstant.USER_LOGIN,"");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return "redirect:loginUI";
    }

}
